﻿using DevExpress.Data;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.UC.面料审核.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.面料审核
{
    public partial class Form面料待处理 : DevExpress.XtraEditors.XtraForm
    {
        private List<WIPBatchWIthMBomDTO> wIPBatchWIthMBomDTOs { get; set; } = new List<WIPBatchWIthMBomDTO>();
        private ShopInfoDTO shopInfoDTO = new ShopInfoDTO();
        private ShopRightQueryFacadeDTO ShopRightQueryFacadeDTO = new ShopRightQueryFacadeDTO();

        public Form面料待处理(List<String> shopIds)
        {
            InitializeComponent();
            this.wIPBatchWIthMBomDTOs = 面料审核Service.getFabricBOMConsumption(shopIds);
            this.gridControl面料待处理一览.DataSource = this.wIPBatchWIthMBomDTOs;
            this.pivotGridControl1.DataSource = this.wIPBatchWIthMBomDTOs;
            //添加表头，面料需求数量统计
            this.gridView面料待处理一览.GroupSummary.AddRange(new GridSummaryItem[] { new GridGroupSummaryItem(SummaryItemType.Sum, "batchQuantity", null, "(数量总计= {0})"), });
            this.gridView面料待处理一览.GroupSummary.AddRange(new GridSummaryItem[] { new GridGroupSummaryItem(SummaryItemType.Sum, "ffixscrapqty", null, "(固定损耗总计= {0})"),});
            this.gridView面料待处理一览.ExpandAllGroups();
        }
        public Form面料待处理(ShopInfoDTO shopInfoDTO, ShopRightQueryFacadeDTO shopRightQueryFacadeDTO)
        {
            InitializeComponent();
            this.ShopRightQueryFacadeDTO = shopRightQueryFacadeDTO;
            this.shopInfoDTO = shopInfoDTO;
            List<String> shopIds = new List<string>();
            shopIds.Add(shopInfoDTO.shopId);
            this.wIPBatchWIthMBomDTOs = 面料审核Service.getFabricBOMConsumption(shopIds);
            this.gridControl面料待处理一览.DataSource = this.wIPBatchWIthMBomDTOs;
            this.pivotGridControl1.DataSource = this.wIPBatchWIthMBomDTOs;
            //添加表头，面料需求数量统计
            this.gridView面料待处理一览.GroupSummary.AddRange(new GridSummaryItem[] { new GridGroupSummaryItem(SummaryItemType.Sum, "batchQuantity", null, "(数量总计= {0})"), });
            this.gridView面料待处理一览.GroupSummary.AddRange(new GridSummaryItem[] { new GridGroupSummaryItem(SummaryItemType.Sum, "ffixscrapqty", null, "(固定损耗总计= {0})"), });
            this.gridView面料待处理一览.ExpandAllGroups();
        }


        private void gridView面料待处理一览_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.Name.Equals("gridColumn尺码"))
            {
                if (!String.IsNullOrWhiteSpace(e.DisplayText))
                {
                    try { e.DisplayText = e.DisplayText.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                }
                else {
                    e.DisplayText = "样衣无尺寸定义";
                }
            }
            else if (e.Column.Name == "gridColumn固定损耗")
            {
                if ("0".Equals(e.CellValue.ToString()))
                {
                    e.Appearance.BackColor = Color.OrangeRed;
                }
            }
            else if (e.Column.Name == "gridColumn待出库标识")
            {
                if ((Boolean)e.CellValue == true)
                {
                    e.Appearance.BackColor = Color.Green;
                }
                else {
                    e.Appearance.BackColor = Color.White;
                }
            }
        }

        private void 刷新()
        {
            this.gridControl面料待处理一览.DataSource = this.wIPBatchWIthMBomDTOs;
            this.gridView面料待处理一览.RefreshData();
            this.gridView面料待处理一览.ExpandAllGroups();

            this.pivotGridControl1.DataSource = this.wIPBatchWIthMBomDTOs;
            this.pivotGridControl1.RefreshData();
        }

        private void gridView面料待处理一览_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                //获取单行DTO
                //选中行时 stockOutFlg 改为true
                String batchNo = this.gridView面料待处理一览.GetRowCellValue(this.gridView面料待处理一览.FocusedRowHandle, "batchNo").ToString();
                //双击后，待出库标识转换，传batchNo
                this.stockOutByBatchNo(batchNo);
                this.刷新();
            }
            catch { }
            
        }

        //待出库标识转换
        private void stockOutByBatchNo(String batchNo) {
            foreach(WIPBatchWIthMBomDTO wIPBatchWIthMBomDTO in this.wIPBatchWIthMBomDTOs) {
                if (batchNo.Equals(wIPBatchWIthMBomDTO.batchNo)) {
                    wIPBatchWIthMBomDTO.stockOutFlg = !wIPBatchWIthMBomDTO.stockOutFlg;
                    break;
                }
            }
        }

        private void textBox扫描生产批号_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyValue == 13)
            {
                this.stockOutByBatchNo(this.textBox扫描生产批号.Text);
                this.刷新();
                this.textBox扫描生产批号.Clear();
            }
        }

        private void pivotGridControl1_CustomCellDisplayText(object sender, DevExpress.XtraPivotGrid.PivotCellDisplayTextEventArgs e)
        {
            //if (e.DataField.FieldName.Equals("stockOutFlg"))
            //{
            //    if (false == (Boolean)e.Value)
            //    {
            //        e.DisplayText = "未出库";
            //    }
            //    else
            //    {
            //        e.DisplayText = "待出库";
            //    }
            //}
        }

        private void simpleButton确认_Click(object sender, EventArgs e)
        {
            //进入预览界面
            new Form面料审核(this.wIPBatchWIthMBomDTOs,this.ShopRightQueryFacadeDTO).ShowDialog();
            this.刷新();
        }

        private void simpleButton批量选择_Click(object sender, EventArgs e)
        {
            foreach(WIPBatchWIthMBomDTO wIPBatchWIthMBomDTO in this.wIPBatchWIthMBomDTOs)
            {
                wIPBatchWIthMBomDTO.stockOutFlg = true;
            }
            this.刷新();
        }
    }
}
